﻿using DataProcessor.DB.ORM;
using Hjt212Parser;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DataProcessor.Processor
{
    public class DataInstructionProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DataInstructionProcessor));
        /// <summary>
        /// 实时数据处理：直接插入，不做重复判断
        /// </summary>
        /// <param name="content"></param>
        public static void Process2011(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }
                if(!dataTime.HasValue) return;
                using (var contextTarget = new PlatformEntities())
                {
                    var rtd = Parser.GetDoubleByKey(masterCode + "-Rtd", content) ?? 0;
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:
                            //实时监控表（水）
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    设备信息ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID
                                };
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }

                            masterMonitorWater.实时数据时间 = dataTime.Value;
                            masterMonitorWater.瞬时量 = rtd;
                            masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                            var masterWater = new RealDataMain
                            {
                                企业基本信息ID = info.企业基本信息ID,
                                设备信息ID = info.ID,
                                污染物类别ID = pollutionInfo.污染物类别ID,
                                监测时间 = dataTime.Value,
                                RTD = rtd
                            };
                            contextTarget.RealDataMains.Add(masterWater);
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                //实时监控从表（水）
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }
                                detailMonitorWater.瞬时量 = Parser.GetDoubleByKey(detailCode + "-Rtd", content) ?? 0;
                                var detailWater = new RealDataDetail()
                                {
                                    污染物编码ID = pollutionInfo.ID,
                                    RTD = Parser.GetDoubleByKey(detailCode + "-Rtd", content) ?? 0,
                                    //ZSRTD = Parser.GetDoubleByKey(detailCode + "-ZsRtd", content),
                                    Flag = Parser.GetStringByKey(detailCode + "-Flag", content)
                                };
                                masterWater.RealDataDetails.Add(detailWater);
                            }
                            break;
                        case EnumFactorType.Gas:
                            //修改实时监控表（气）
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气
                                {
                                    企业基本信息ID = info.企业基本信息ID, 企业排口ID = info.ID
                                };
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }
                            masterMonitorGas.实时数据时间 = dataTime.Value;
                            masterMonitorGas.瞬时量 = rtd;

                            //插入实时表
                            var masterGas = new 主实时数据气
                            {
                                企业基本信息ID = info.企业基本信息ID,
                                企业排口ID = info.ID,
                                污染物类别ID = pollutionInfo.污染物类别ID,
                                监测时间 = dataTime.Value,
                                RTD = rtd
                            };
                            contextTarget.主实时数据气.Add(masterGas);

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                //实时监控从表（气）
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }
                                detailMonitorGas.瞬时量 = Parser.GetDoubleByKey(detailCode + "-Rtd", content) ?? 0;
                                detailMonitorGas.折算瞬时量 = Parser.GetDoubleByKey(detailCode + "-ZsRtd", content);

                                //实时表从表
                                var detailGas = new 从实时数据气()
                                {
                                    污染物编码ID = pollutionInfo.ID,
                                    RTD = Parser.GetDoubleByKey(detailCode + "-Rtd", content) ?? 0,
                                    ZSRTD = Parser.GetDoubleByKey(detailCode + "-ZsRtd", content),
                                    Flag = Parser.GetStringByKey(detailCode + "-Flag", content)
                                };
                                masterGas.从实时数据气.Add(detailGas);
                            }
                            break;
                    }
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }
        /// <summary>
        /// 预处理：查看相关信息是否有问题。
        /// </summary>
        /// <param name="content"></param>
        /// <param name="info"></param>
        /// <param name="dataTime"></param>
        /// <param name="pollutantCodes"></param>
        /// <param name="masterCode"></param>
        /// <param name="pollutionInfo"></param>
        /// <returns></returns>
        public static bool HasQuestionOfPreprocess(string content, out EquipmentInfor info, out DateTime? dataTime, out List<string> pollutantCodes,
            out string masterCode, out MonitorFactorCode pollutionInfo)
        {
            info = null;
            dataTime = null;
            pollutantCodes = null;
            masterCode = null;
            pollutionInfo = null;
            var mn = Parser.GetStringByKey("MN", content);

            if (string.IsNullOrWhiteSpace(mn))
            {
                Log.Warn("MN 号为空.");
                return true;
            }
            info = Util.GetInfoFromMn(mn);
            if (info == null)
            {
                Log.Warn("MN：" + mn + ",找不到对应企业！");
                return true;
            }
            dataTime = Parser.GetDateTimeByKey("DataTime", content);
            if (!dataTime.HasValue)
            {
                Log.Warn("数据时间为空或格式不对.");
                return true;
            }
            pollutantCodes = Parser.GetPollutantCodes(content);
            if (pollutantCodes == null || pollutantCodes.Count < 1)
            {
                Log.Warn("没有污染物因子.");
                return true;
            }
            masterCode = Util.GetMasterPollutionCode(pollutantCodes);
            if (masterCode == null)
            {
                Log.Warn("没有主污染物因子.");
                return true;
            }
            pollutionInfo = Util.GetContaminantByCode(masterCode);
            if (pollutionInfo == null)
            {
                Log.Warn("平台没有设置污染物 :" + masterCode);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 分钟数据处理:直接插入，不做重复判断
        /// </summary>
        /// <param name="content"></param>
        public static void Process2051(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }

                if(!dataTime.HasValue)return;
                using (var contextTarget = new PlatformEntities())
                {
                    var cou = Parser.GetDoubleByKey(masterCode + "-Cou", content) ?? 0;
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:
                            //实时监控水
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain();
                                masterMonitorWater.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorWater.设备信息ID = info.ID;
                                masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }

                            masterMonitorWater.分钟数据时间 = dataTime.Value;
                            masterMonitorWater.分钟排放量 = cou;
                            masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                            var masterWater = new MinuteDataMain()
                            {
                                企业基本信息ID = info.企业基本信息ID,
                                设备信息ID = info.ID,
                                污染物类别ID = pollutionInfo.污染物类别ID,
                                监测时间 = dataTime.Value,
                                时段量 = cou
                            };
                            contextTarget.MinuteDataMains.Add(masterWater);

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                //实时监控从表（水）
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail();
                                    detailMonitorWater.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }
                                detailMonitorWater.分钟排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);

                                var detailWater = new MinuteDataDetail()
                                {
                                    污染物编码ID = pollutionInfo.ID,
                                    Cou = Parser.GetDoubleByKey(detailCode + "-Cou", content),
                                    Min = Parser.GetDoubleByKey(detailCode + "-Min", content),
                                    Avg = Parser.GetDoubleByKey(detailCode + "-Avg", content),
                                    Max = Parser.GetDoubleByKey(detailCode + "-Max", content),
                                };
                                masterWater.MinuteDataDetails.Add(detailWater);
                            }
                            break;
                        case EnumFactorType.Gas:
                            //实时监控气
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气();
                                masterMonitorGas.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorGas.企业排口ID = info.ID;
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }
                            masterMonitorGas.分钟数据时间 = dataTime.Value;
                            masterMonitorGas.分钟排放量 = cou;

                            var masterGas = new 主分钟数据气()
                            {
                                企业基本信息ID = info.企业基本信息ID,
                                企业排口ID = info.ID,
                                污染物类别ID = pollutionInfo.污染物类别ID,
                                监测时间 = dataTime.Value,
                                时段量 = cou
                            };
                            contextTarget.主分钟数据气.Add(masterGas);

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }
                                detailMonitorGas.分钟排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);

                                var detailGas = new 从分钟数据气()
                                {
                                    污染物编码ID = pollutionInfo.ID,
                                    Cou = Parser.GetDoubleByKey(detailCode + "-Cou", content),
                                    ZSCou = Parser.GetDoubleByKey(detailCode + "-ZsCou", content),
                                    Min = Parser.GetDoubleByKey(detailCode + "-Min", content),
                                    ZSMin = Parser.GetDoubleByKey(detailCode + "-ZsMin", content),
                                    Avg = Parser.GetDoubleByKey(detailCode + "-Avg", content),
                                    ZSAvg = Parser.GetDoubleByKey(detailCode + "-ZsAvg", content),
                                    Max = Parser.GetDoubleByKey(detailCode + "-Max", content),
                                    ZSMax = Parser.GetDoubleByKey(detailCode + "-ZsMax", content)
                                };
                                masterGas.从分钟数据气.Add(detailGas);
                            }
                            break;
                    }

                    contextTarget.SaveChanges();
                }
                Log.Info("处理成功" + "：\r\n" + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }
        /// <summary>
        /// 小时数据处理：避免重复
        /// </summary>
        /// <param name="content"></param>
        public static void Process2061(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }
                if (!dataTime.HasValue) return;
                var begin = new DateTime(dataTime.Value.Year, dataTime.Value.Month, dataTime.Value.Day, dataTime.Value.Hour, 0, 0);
                var end = begin.AddHours(1);
                using (var contextTarget = new PlatformEntities())
                {
                    var cou = Parser.GetDoubleByKey(masterCode + "-Cou", content) ?? 0;
                   
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:

                            //实时监控水
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain();
                                masterMonitorWater.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorWater.设备信息ID = info.ID;
                                masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }

                            masterMonitorWater.小时数据时间 = dataTime.Value;
                            masterMonitorWater.小时排放量 = cou;
                            masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;

                            var masterWater = contextTarget.HourDataMains
                                .Where(t => t.设备信息ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterWater == null)
                            {
                                masterWater = new HourDataMain()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    设备信息ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.HourDataMains.Add(masterWater);
                            }

                            masterWater.监测时间 = dataTime.Value;
                            masterWater.时段量 = cou;
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }

                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                //实时监控从表（水）
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail();
                                    detailMonitorWater.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }
                                detailMonitorWater.小时排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                                var detailWater = masterWater.HourDataDetails
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailWater == null)
                                {
                                    detailWater = new HourDataDetail()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterWater.HourDataDetails.Add(detailWater);
                                }

                                detailWater.Cou = Parser.GetDoubleByKey(detailCode + "-Cou", content); 
                                detailWater.Min = Parser.GetDoubleByKey(detailCode + "-Min", content);
                                detailWater.Avg = Parser.GetDoubleByKey(detailCode + "-Avg", content);
                                detailWater.Max = Parser.GetDoubleByKey(detailCode + "-Max", content);
                            }

                            break;
                        case EnumFactorType.Gas:
                            //实时监控气
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气();
                                masterMonitorGas.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorGas.企业排口ID = info.ID;
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }
                            masterMonitorGas.小时数据时间 = dataTime.Value;
                            masterMonitorGas.小时排放量 = cou;

                            var masterGas = contextTarget.主小时数据气
                                .Where(t => t.企业排口ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterGas == null)
                            {
                                masterGas = new 主小时数据气()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    企业排口ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.主小时数据气.Add(masterGas);
                            }
                            masterGas.监测时间 = dataTime.Value;
                            masterGas.时段量 = cou;
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }
                                detailMonitorGas.小时排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content); 

                                var detailGas = masterGas.从小时数据气
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailGas == null)
                                {
                                    detailGas = new 从小时数据气()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterGas.从小时数据气.Add(detailGas);
                                }

                                detailGas.Cou = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                                detailGas.ZSCou = Parser.GetDoubleByKey(detailCode + "-ZsCou", content);
                                detailGas.Min = Parser.GetDoubleByKey(detailCode + "-Min", content);
                                detailGas.ZSMin = Parser.GetDoubleByKey(detailCode + "-ZsMin", content);
                                detailGas.Avg = Parser.GetDoubleByKey(detailCode + "-Avg", content);
                                detailGas.ZSAvg = Parser.GetDoubleByKey(detailCode + "-ZsAvg", content);
                                detailGas.Max = Parser.GetDoubleByKey(detailCode + "-Max", content);
                                detailGas.ZSMax = Parser.GetDoubleByKey(detailCode + "-ZsMax", content);
                            }
                            break;
                    }

                    contextTarget.SaveChanges();
                }
                Log.Info("处理成功" + "：\r\n" + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }
        public static void ProcessHourToDay(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }
                if (!dataTime.HasValue) return;
                var begin = new DateTime(dataTime.Value.Year, dataTime.Value.Month, dataTime.Value.Day, 0, 0, 0);
                var end = begin.AddDays(1);
                using (var contextTarget = new PlatformEntities())
                {
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:

                            //实时监控水
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain();
                                masterMonitorWater.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorWater.设备信息ID = info.ID;
                                masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }

                            var masterWaterRecords = contextTarget.HourDataMains.Where(t => t.设备信息ID == info.ID)
                                .Where(t => t.监测时间 >= begin && t.监测时间 < end).ToList();
                            var masterWaterIds = masterWaterRecords.Select(t => t.ID).ToList();
                            var masterWater = contextTarget.DayDataMains
                                .Where(t => t.设备信息ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterWater == null)
                            {
                                masterWater = new DayDataMain()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    设备信息ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID
                                };
                                contextTarget.DayDataMains.Add(masterWater);
                            }

                            masterWater.监测时间 = begin;
                            masterWater.时段量 = masterWaterRecords.Sum(t => t.时段量);

                            masterMonitorWater.日数据时间 = masterWater.监测时间;
                            masterMonitorWater.日排放量 = masterWater.时段量;

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                //实时监控从表（水）
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail();
                                    detailMonitorWater.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }

                                var detailWaterRecords = contextTarget.HourDataDetails
                                    .Where(t => masterWaterIds.Contains(t.主小时数据ID)).Where(t => t.污染物编码ID == pollutionInfo.ID)
                                    .ToList();
                                var detailWater = masterWater.DayDataDetails
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailWater == null)
                                {
                                    detailWater = new DayDataDetail()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterWater.DayDataDetails.Add(detailWater);
                                }

                                detailWater.Cou = detailWaterRecords.Sum(t => t.Cou);
                                detailWater.Min = detailWaterRecords.Min(t => t.Min);
                                detailWater.Avg = detailWaterRecords.Average(t => t.Avg);
                                detailWater.Max = detailWaterRecords.Max(t => t.Max);

                                detailMonitorWater.日排放量 = detailWater.Cou;

                            }

                            break;
                        case EnumFactorType.Gas:

                            //实时监控气
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气();
                                masterMonitorGas.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorGas.企业排口ID = info.ID;
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }
                          
                            var masterGasRecords = contextTarget.主小时数据气.Where(t => t.企业排口ID == info.ID)
                                .Where(t => t.监测时间 >= begin && t.监测时间 < end).ToList();
                            var masterGasIds = masterGasRecords.Select(t => t.ID).ToList();

                            var masterGas = contextTarget.主日数据气
                                .Where(t => t.企业排口ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterGas == null)
                            {
                                masterGas = new 主日数据气()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    企业排口ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.主日数据气.Add(masterGas);
                            }
                            masterGas.监测时间 = begin;
                            masterGas.时段量 = masterGasRecords.Sum(t => t.时段量);

                            masterMonitorGas.日数据时间 = masterGas.监测时间;
                            masterMonitorGas.日排放量 = masterGas.时段量;


                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }

                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }
                               

                                var detailGasRecords = contextTarget.从小时数据气
                                    .Where(t => masterGasIds.Contains(t.主小时数据ID)).Where(t => t.污染物编码ID == pollutionInfo.ID)
                                    .ToList();

                                var detailGas = masterGas.从日数据气
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailGas == null)
                                {
                                    detailGas = new 从日数据气()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterGas.从日数据气.Add(detailGas);
                                }

                                detailGas.Cou = detailGasRecords.Sum(t => t.Cou);
                                detailGas.ZSCou = detailGasRecords.Sum(t => t.ZSCou);
                                detailGas.Min = detailGasRecords.Min(t => t.Min);
                                detailGas.ZSMin = detailGasRecords.Min(t => t.ZSMin);
                                detailGas.Avg = detailGasRecords.Average(t => t.Avg);
                                detailGas.ZSAvg = detailGasRecords.Average(t => t.ZSAvg);
                                detailGas.Max = detailGasRecords.Min(t => t.Min);
                                detailGas.ZSMax = detailGasRecords.Min(t => t.ZSMin);

                                detailMonitorGas.日排放量 = detailGas.Cou;
                            }
                            break;
                    }

                    contextTarget.SaveChanges();
                }
                Log.Info("处理成功" + "：\r\n" + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }
        /// <summary>
        /// 日数据处理：避免重复
        /// </summary>
        /// <param name="content"></param>
        public static void Process2031(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }
                if (!dataTime.HasValue) return;
                var begin = new DateTime(dataTime.Value.Year, dataTime.Value.Month, dataTime.Value.Day, 0, 0, 0);
                var end = begin.AddDays(1);
                using (var contextTarget = new PlatformEntities())
                {
                    var cou = Parser.GetDoubleByKey(masterCode + "-Cou", content);
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:
                            //实时监控水
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain();
                                masterMonitorWater.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorWater.设备信息ID = info.ID;
                                masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }
                            masterMonitorWater.日数据时间 = dataTime.Value;
                            masterMonitorWater.日排放量 = cou;
                            masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                            var masterWater = contextTarget.DayDataMains
                                .Where(t => t.设备信息ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterWater == null)
                            {
                                masterWater = new DayDataMain()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    设备信息ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.DayDataMains.Add(masterWater);
                            }

                            masterWater.监测时间 = dataTime.Value;
                            masterWater.时段量 = cou;
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                //实时监控从表（水）
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail();
                                    detailMonitorWater.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }
                                detailMonitorWater.分钟排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);

                                var detailWater = masterWater.DayDataDetails
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailWater == null)
                                {
                                    detailWater = new DayDataDetail()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterWater.DayDataDetails.Add(detailWater);
                                }

                                detailWater.Cou = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                                detailWater.Min = Parser.GetDoubleByKey(detailCode + "-Min", content);
                                detailWater.Avg = Parser.GetDoubleByKey(detailCode + "-Avg", content);
                                detailWater.Max = Parser.GetDoubleByKey(detailCode + "-Max", content);
                            }

                            break;
                        case EnumFactorType.Gas:
                            //实时监控气
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气();
                                masterMonitorGas.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorGas.企业排口ID = info.ID;
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }
                            masterMonitorGas.日数据时间 = dataTime.Value;
                            masterMonitorGas.日排放量 = cou;

                            var masterGas = contextTarget.主日数据气
                                .Where(t => t.企业排口ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterGas == null)
                            {
                                masterGas = new 主日数据气()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    企业排口ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.主日数据气.Add(masterGas);
                            }
                            masterGas.监测时间 = dataTime.Value;
                            masterGas.时段量 = Parser.GetDoubleByKey(masterCode + "-Cou", content);
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }
                                detailMonitorGas.日排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);

                                var detailGas = masterGas.从日数据气
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailGas == null)
                                {
                                    detailGas = new 从日数据气()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterGas.从日数据气.Add(detailGas);
                                }

                                detailGas.Cou = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                                detailGas.ZSCou = Parser.GetDoubleByKey(detailCode + "-ZsCou", content);
                                detailGas.Min = Parser.GetDoubleByKey(detailCode + "-Min", content);
                                detailGas.ZSMin = Parser.GetDoubleByKey(detailCode + "-ZsMin", content);
                                detailGas.Avg = Parser.GetDoubleByKey(detailCode + "-Avg", content);
                                detailGas.ZSAvg = Parser.GetDoubleByKey(detailCode + "-ZsAvg", content);
                                detailGas.Max = Parser.GetDoubleByKey(detailCode + "-Max", content);
                                detailGas.ZSMax = Parser.GetDoubleByKey(detailCode + "-ZsMax", content);
                            }
                            break;
                    }

                    contextTarget.SaveChanges();
                }
                Log.Info("处理成功" + "：\r\n" + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }
        public static void ProcessDayToMonth(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }
                if (!dataTime.HasValue) return;
                var begin = new DateTime(dataTime.Value.Year, dataTime.Value.Month, 1, 0, 0, 0);
                var end = begin.AddMonths(1);
                using (var contextTarget = new PlatformEntities())
                {
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:
                            //实时监控水
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain();
                                masterMonitorWater.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorWater.设备信息ID = info.ID;
                                masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }

                            var masterWaterRecords = contextTarget.DayDataMains.Where(t => t.设备信息ID == info.ID)
                                .Where(t => t.监测时间 >= begin && t.监测时间 < end).ToList();
                            var masterWaterIds = masterWaterRecords.Select(t => t.ID).ToList();
                            var masterWater = contextTarget.MonthDataMains
                                .Where(t => t.设备信息ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterWater == null)
                            {
                                masterWater = new MonthDataMain()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    设备信息ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID
                                };
                                contextTarget.MonthDataMains.Add(masterWater);
                            }

                            masterWater.监测时间 = begin;
                            masterWater.时段量 = masterWaterRecords.Sum(t => t.时段量);

                            masterMonitorWater.月数据时间 = masterWater.监测时间;
                            masterMonitorWater.月排放量 = masterWater.时段量;

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                //实时监控从表（水）
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail();
                                    detailMonitorWater.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }

                                var detailWaterRecords = contextTarget.DayDataDetails
                                    .Where(t => masterWaterIds.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == pollutionInfo.ID)
                                    .ToList();
                                var detailWater = masterWater.MonthDataDetails
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailWater == null)
                                {
                                    detailWater = new MonthDataDetail()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterWater.MonthDataDetails.Add(detailWater);
                                }

                                detailWater.Cou = detailWaterRecords.Sum(t => t.Cou);
                                detailWater.Min = detailWaterRecords.Min(t => t.Min);
                                detailWater.Avg = detailWaterRecords.Average(t => t.Avg);
                                detailWater.Max = detailWaterRecords.Max(t => t.Max);

                                detailMonitorWater.月排放量 = detailWater.Cou;
                            }

                            break;
                        case EnumFactorType.Gas:
                            //实时监控气
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气();
                                masterMonitorGas.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorGas.企业排口ID = info.ID;
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }

                            var masterGasRecords = contextTarget.主日数据气.Where(t => t.企业排口ID == info.ID)
                                .Where(t => t.监测时间 >= begin && t.监测时间 < end).ToList();
                            var masterGasIds = masterGasRecords.Select(t => t.ID).ToList();

                            var masterGas = contextTarget.主月数据气
                                .Where(t => t.企业排口ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterGas == null)
                            {
                                masterGas = new 主月数据气()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    企业排口ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.主月数据气.Add(masterGas);
                            }
                            masterGas.监测时间 = begin;
                            masterGas.时段量 = masterGasRecords.Sum(t => t.时段量);

                            masterMonitorGas.月数据时间 = masterGas.监测时间;
                            masterMonitorGas.月排放量 = masterGas.时段量;

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }

                                var detailGasRecords = contextTarget.从日数据气
                                    .Where(t => masterGasIds.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == pollutionInfo.ID)
                                    .ToList();

                                var detailGas = masterGas.从月数据气
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailGas == null)
                                {
                                    detailGas = new 从月数据气()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterGas.从月数据气.Add(detailGas);
                                }

                                detailGas.Cou = detailGasRecords.Sum(t => t.Cou);
                                detailGas.ZSCou = detailGasRecords.Sum(t => t.ZSCou);
                                detailGas.Min = detailGasRecords.Min(t => t.Min);
                                detailGas.ZSMin = detailGasRecords.Min(t => t.ZSMin);
                                detailGas.Avg = detailGasRecords.Average(t => t.Avg);
                                detailGas.ZSAvg = detailGasRecords.Average(t => t.ZSAvg);
                                detailGas.Max = detailGasRecords.Min(t => t.Min);
                                detailGas.ZSMax = detailGasRecords.Min(t => t.ZSMin);

                                detailMonitorGas.月排放量 = detailGas.Cou;
                            }
                            break;
                    }

                    contextTarget.SaveChanges();
                }
                Log.Info("处理成功" + "：\r\n" + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }
        public static void ProcessMonthToYear(string content)
        {
            try
            {
                if (HasQuestionOfPreprocess(content, out var info, out var dataTime, out var pollutantCodes, out var masterCode, out var pollutionInfo))
                {
                    return;
                }
                if (!dataTime.HasValue) return;
                var begin = new DateTime(dataTime.Value.Year, 1, 1, 0, 0, 0);
                var end = begin.AddYears(1);
                using (var contextTarget = new PlatformEntities())
                {
                    switch (FactorTypeHelper.GetFactorType(content))
                    {
                        case EnumFactorType.Water:
                            //实时监控水
                            var masterMonitorWater = contextTarget.RealDataMonitorMains
                                .FirstOrDefault(t => t.设备信息ID == info.ID);
                            if (masterMonitorWater == null)
                            {
                                masterMonitorWater = new RealDataMonitorMain();
                                masterMonitorWater.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorWater.设备信息ID = info.ID;
                                masterMonitorWater.污染物类别ID = pollutionInfo.污染物类别ID;
                                contextTarget.RealDataMonitorMains.Add(masterMonitorWater);
                            }

                            var masterWaterRecords = contextTarget.MonthDataMains.Where(t => t.设备信息ID == info.ID)
                                .Where(t => t.监测时间 >= begin && t.监测时间 < end).ToList();
                            var masterWaterIds = masterWaterRecords.Select(t => t.ID).ToList();
                            var masterWater = contextTarget.YearDataMains
                                .Where(t => t.设备信息ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterWater == null)
                            {
                                masterWater = new YearDataMain()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    设备信息ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID
                                };
                                contextTarget.YearDataMains.Add(masterWater);
                            }

                            masterWater.监测时间 = begin;
                            masterWater.时段量 = masterWaterRecords.Sum(t => t.时段量);

                            masterMonitorWater.年数据时间 = masterWater.监测时间;
                            masterMonitorWater.年排放量 = masterWater.时段量;


                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                //实时监控从表（水）
                                var detailMonitorWater =
                                    masterMonitorWater.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);

                                if (detailMonitorWater == null)
                                {
                                    detailMonitorWater = new RealDataMonitorDetail();
                                    detailMonitorWater.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorWater.RealDataMonitorDetails.Add(detailMonitorWater);
                                }

                                var detailWaterRecords = contextTarget.MonthDataDetails
                                    .Where(t => masterWaterIds.Contains(t.主月数据ID)).Where(t => t.污染物编码ID == pollutionInfo.ID)
                                    .ToList();
                                var detailWater = masterWater.YearDataDetails
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailWater == null)
                                {
                                    detailWater = new YearDataDetail()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterWater.YearDataDetails.Add(detailWater);
                                }

                                detailWater.Cou = detailWaterRecords.Sum(t => t.Cou);
                                detailWater.Min = detailWaterRecords.Min(t => t.Min);
                                detailWater.Avg = detailWaterRecords.Average(t => t.Avg);
                                detailWater.Max = detailWaterRecords.Max(t => t.Max);

                                detailMonitorWater.年排放量 = detailWater.Cou;
                            }

                            break;
                        case EnumFactorType.Gas:

                            //实时监控气
                            var masterMonitorGas =
                                contextTarget.主实时监控气.FirstOrDefault(t => t.企业排口ID == info.ID);
                            if (masterMonitorGas == null)
                            {
                                masterMonitorGas = new 主实时监控气();
                                masterMonitorGas.企业基本信息ID = info.企业基本信息ID;
                                masterMonitorGas.企业排口ID = info.ID;
                                contextTarget.主实时监控气.Add(masterMonitorGas);
                            }

                            var masterGasRecords = contextTarget.主月数据气.Where(t => t.企业排口ID == info.ID)
                                .Where(t => t.监测时间 >= begin && t.监测时间 < end).ToList();
                            var masterGasIds = masterGasRecords.Select(t => t.ID).ToList();

                            var masterGas = contextTarget.主年数据气
                                .Where(t => t.企业排口ID == info.ID).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                            if (masterGas == null)
                            {
                                masterGas = new 主年数据气()
                                {
                                    企业基本信息ID = info.企业基本信息ID,
                                    企业排口ID = info.ID,
                                    污染物类别ID = pollutionInfo.污染物类别ID

                                };
                                contextTarget.主年数据气.Add(masterGas);
                            }
                            masterGas.监测时间 = begin;
                            masterGas.时段量 = masterGasRecords.Sum(t => t.时段量) ?? 0;

                            masterMonitorGas.年数据时间 = masterGas.监测时间;
                            masterMonitorGas.年排放量 = masterGas.时段量;

                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("平台没有设置污染物 :" + detailCode);
                                    continue;
                                }
                                var detailMonitorGas =
                                    masterMonitorGas.从实时监控气.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitorGas == null)
                                {
                                    detailMonitorGas = new 从实时监控气();
                                    detailMonitorGas.污染物编码ID = pollutionInfo.ID;
                                    masterMonitorGas.从实时监控气.Add(detailMonitorGas);
                                }

                                var detailGasRecords = contextTarget.从月数据气
                                    .Where(t => masterGasIds.Contains(t.主月数据ID)).Where(t => t.污染物编码ID == pollutionInfo.ID)
                                    .ToList();

                                var detailGas = masterGas.从年数据气
                                    .FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailGas == null)
                                {
                                    detailGas = new 从年数据气()
                                    {
                                        污染物编码ID = pollutionInfo.ID
                                    };
                                    masterGas.从年数据气.Add(detailGas);
                                }

                                detailGas.Cou = detailGasRecords.Sum(t => t.Cou);
                                detailGas.ZSCou = detailGasRecords.Sum(t => t.ZSCou);
                                detailGas.Min = detailGasRecords.Min(t => t.Min);
                                detailGas.ZSMin = detailGasRecords.Min(t => t.ZSMin);
                                detailGas.Avg = detailGasRecords.Average(t => t.Avg);
                                detailGas.ZSAvg = detailGasRecords.Average(t => t.ZSAvg);
                                detailGas.Max = detailGasRecords.Min(t => t.Min);
                                detailGas.ZSMax = detailGasRecords.Min(t => t.ZSMin);

                                detailMonitorGas.年排放量 = detailGas.Cou;
                            }
                            break;
                    }

                    contextTarget.SaveChanges();
                }
                Log.Info("处理成功" + "：\r\n" + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex + "->\n" + content);
            }
        }

    }
}
